package com.byx.chatclient.adapter;

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.byx.chatclient.R;
import com.byx.chatclient.bean.ChooseGroupBean;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * ChooseGroupAdapter
 * 选择分组列表适配器
 * @author Superb
 * @date 2021/5/1 15:51
 */
public class ChooseGroupAdapter extends BaseQuickAdapter<ChooseGroupBean, BaseViewHolder> {
    private static final int LAY_RES_ID = R.layout.layout_choose_group_item;
    /**
     * 上一次点击的项索引
     */
    private int oldPos;
    private OnItemSelectedListener onItemSelectedListener;

    public ChooseGroupAdapter(@Nullable List<ChooseGroupBean> data) {
        super(LAY_RES_ID, data);
        init();
    }

    public ChooseGroupAdapter() {
        super(LAY_RES_ID);
        init();
    }

    private void init(){
        setOnItemClickListener((adapter, view, position) -> {
            if (position != oldPos){
                ((ImageView) adapter.getViewByPosition(oldPos, R.id.iv_choose)).setImageDrawable(null);
                ((ImageView) adapter.getViewByPosition(position, R.id.iv_choose)).setImageResource(R.mipmap.ic_check_box_checked_64);
                oldPos = position;
            }
            if (onItemSelectedListener != null){
                onItemSelectedListener.onItemSelected(position);
            }
        });
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, ChooseGroupBean chooseGroupBean) {
        baseViewHolder.setText(R.id.tv_group_name,chooseGroupBean.getGroupName());
    }

    public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
        this.onItemSelectedListener = onItemSelectedListener;
    }

    public interface OnItemSelectedListener{
        /**
         * 选中监听
         * @param position
         */
        void onItemSelected(int position);
    }
}
